package com.android.widget;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.io.File;
import java.util.List;

/**
 * Created by Relin
 * on 2018-10-09.
 */
public class BannerAdapter<T> extends PagerAdapter {

    private Context context;
    private List<T> data;
    private View convertView;
    private int position;
    private boolean isLoop = true;
    private BannerPager.OnPageClickListener listener;
    private OnMeasureConvertViewListener onMeasureListener;

    public BannerAdapter(Context context, List<T> data) {
        this.context = context;
        this.data = data;
    }

    public void setLoop(boolean isLoop) {
        this.isLoop = isLoop;
        if (getCount() > 0 && isLoop) {
            this.data.add(0, data.get((getCount() - 1)));
            this.data.add(data.get(1));
        }
    }

    public boolean isLoop() {
        return isLoop;
    }

    public Context getContext() {
        return context;
    }

    public List<T> getData() {
        return data;
    }

    public int getPosition() {
        return position;
    }

    public T getItem(int position) {
        return data.get(position);
    }

    public View getConvertView() {
        return convertView;
    }

    public BannerPager.OnPageClickListener getListener() {
        return listener;
    }

    public void setOnPageClickListener(BannerPager.OnPageClickListener listener) {
        this.listener = listener;
    }

    public void setOnMeasureConvertViewListener(OnMeasureConvertViewListener onMeasureListener) {
        this.onMeasureListener = onMeasureListener;
    }

    public interface OnMeasureConvertViewListener {
        void onMeasureConvertView(BannerAdapter adapter, View convertView);
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        this.position = position;
        convertView = getView(LayoutInflater.from(context), position, null, container);
        container.addView(convertView);
        if (onMeasureListener != null) {
            onMeasureListener.onMeasureConvertView(this, convertView);
        }
        return convertView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        this.position = position;
        container.removeView((View) object);
    }

    protected View getView(LayoutInflater inflater, final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = new ImageView(context);
            holder.target = (ImageView) convertView;
            holder.target.setScaleType(ImageView.ScaleType.FIT_XY);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Object object = data.get(position);
        if (object instanceof Integer) {
            holder.target.setImageResource((int) object);
        }
        if (object instanceof File) {
            File file = (File) object;
            holder.target.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
        }
        if (object instanceof String) {
            holder.target.setTag(null);
            holder.target.setImageBitmap(BitmapFactory.decodeFile((String) object));
        }
        if (object instanceof Uri) {
            holder.target.setImageURI((Uri) object);
        }
        convertView = holder.target;
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onPageClick(isLoop ? position - 1 : position);
                }
            }
        });
        return convertView;
    }

    public class ViewHolder {
        private ImageView target;
    }


}
